<template>
  <div class="search-container">
    <input
      type="text"
      class="city-input"
      v-model="searchCity"
      placeholder="请输入城市名称"
    />
    <div class="cityList">
      <div v-if="cityList.length > 0" class="cityBox">
        <div
          v-for="item in cityList"
          :key="item.adcode"
          class="cityItem"
          @click="handleclick(item.adcode)"
        >
          <p>{{ item.city }}</p>
        </div>
      </div>
      <p class="tip-text" v-if="localList.length ===0">
        暂时没有保存过城市天气信息。请查询后点击右上角"+"号保存。
      </p>
    </div>
    <div class="locallist" v-if="localList.length > 0">
      <div class="local-cities">
        <div class="box" v-for="item in localList" :key="item.adcode">
          <div class="local-item">
            <div class="city-info">
              <span class="city-name">{{ item.city }}</span>
              <span class="temperature">{{ item.temperature }}℃</span>
            </div>
          </div>
          <div class="action-buttons">
            <button class="view-btn" @click="handleclick(item.adcode)">
              查看
            </button>
            <button class="delete-btn" @click="handleDelete(item.adcode)">
              删除
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from "vue";
import { getAdCode, getWeather } from "../api/index.js";
import { useRouter } from "vue-router";
const router = useRouter();
const searchCity = ref("");
const cityList = ref([]);
const localList = ref([]);

// 获取本地存储的城市列表
const getLocalCities = async () => {
  const savedCities = JSON.parse(localStorage.getItem("savedCities")) || [];
  const cities = [];

  // 获取每个保存的城市的详细信息
  for (const adcode of savedCities) {
    try {
      const weatherRes = await getWeather(adcode);
      if (weatherRes.lives && weatherRes.lives[0]) {
        cities.push({
          adcode: adcode,
          city: weatherRes.lives[0].city,
          temperature: weatherRes.lives[0].temperature,
        });
      }
    } catch (error) {
      console.error("获取城市信息失败:", error);
    }
  }

  localList.value = cities;
};

// 删除城市
const handleDelete = (adcode) => {
  const savedCities = JSON.parse(localStorage.getItem("savedCities")) || [];
  const newSavedCities = savedCities.filter((item) => item !== adcode);
  localStorage.setItem("savedCities", JSON.stringify(newSavedCities));
  getLocalCities(); // 重新获取城市列表
};

onMounted(() => {
  getLocalCities();
});

watch(searchCity, async () => {
  if (searchCity.value) {
    const res = await getAdCode(searchCity.value);
    cityList.value = res.geocodes;
    console.log(cityList);
  } else {
    cityList.value = [];
  }
});

const handleclick = (adcode) => {
  router.push({ path: `${adcode}`, query: { adcode } });
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.search-container {
  padding: 20px;

  .city-input {
    width: 100%;
    padding: 12px;
    font-size: 16px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    background: transparent;
    color: white;
    outline: none;
    transition: box-shadow 0.3s ease;

    &:focus {
      box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
    }

    &::placeholder {
      color: rgba(255, 255, 255, 0.7);
    }
  }

  .cityList {
    width: 100%;
    margin-top: 2px;

    .cityBox {
      background: rgba(0, 78, 102, 0.9);
      border-radius: 4px;
      overflow: hidden;
    }

    .cityItem {
      padding: 12px;
      color: white;
      transition: background-color 0.3s ease;
      cursor: pointer;

      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }

      p {
        margin: 0;
        font-size: 16px;
      }
    }
  }

  .locallist {
    .local-cities {
      .box {
        position: relative;
        
        &:hover {
          .action-buttons {
            display: flex;
          }
          
          .local-item {
            width: calc(100% - 120px);  // 为按钮留出空间
          }
        }
        
        .action-buttons {
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          display: none;
          gap: 10px;

          button {
            padding: 4px 12px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s ease;
          }

          .view-btn {
            background-color: #ffd700;
            color: #000;
            &:hover {
              background-color: #ffed4a;
            }
          }

          .delete-btn {
            background-color: #ffd700;
            color: #000;
            &:hover {
              background-color: #ffed4a;
            }
          }
        }
        
        .local-item {
          color: white;
          display: flex;
          align-items: center;
          justify-content: space-between;
          border-radius: 4px;
          transition: all 0.3s ease;
          margin-top: 20px;
          background: rgba(0, 78, 102, 0.9);  
          padding: 15px;
          width: 100%;  // 默认宽度100%

          .city-info {
            flex: 1;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 16px;
            margin-right: 10px;

            .temperature {
              color: rgba(255, 255, 255, 0.8);
              font-size: 14px;
            }
          }
        }
      }
    }
  }

  .tip-text {
    margin-top: 20px;
    color: white;
    text-align: center;
    font-size: 14px;
  }
}
</style>
